package com.lushuan.ch06_springboot_web_rest.controller;


import com.lushuan.ch06_springboot_web_rest.entity.ResponseMessage;
import com.lushuan.ch06_springboot_web_rest.entity.User;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {



    //RestTemplate
    private final RestTemplate restTemplate = new RestTemplate();

    @GetMapping("/{userId}")
    public String order(@PathVariable Integer userId) {

        String url = "http://localhost:8088/user/"+userId;
        ResponseMessage responseMessage = restTemplate.getForObject(url, ResponseMessage.class);
        System.out.println(responseMessage.getData());
        return responseMessage.getData().toString();

    }

    @GetMapping("/add")
    public String add() {

        String url = "http://localhost:8088/user";
        User user = new User("马猴", "四川");
        //url:请求的远程rest url
        // object :post请求的参数
        //Class<T>:返回的类型
        //...0bject:是@pathPathVariable 占位符的参数
        ResponseEntity<ResponseMessage> responseMessage = restTemplate.postForEntity(url, user,ResponseMessage.class);
        System.out.println(responseMessage.getBody().getData());
        // 同样可以使用 restTemplate.exchange() 来调用

        return responseMessage.getBody().getData().toString();

    }



}
